﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Money.Interfaces;
using One.Framework;
using Money.Data;
using One.Web.UI.WebControls;
using One.Web;
using Resources;
using Money.Entities;

public partial class Admin_EditAccount : System.Web.UI.Page
{
    #region fields

    private IAccountService accountService;
    private IAccountTypeService accountTypeService;
    private ICurrencyService currencyService;
    private int accountId;
    private Account account = null;
    private List<AccountType> accountTypes;
    private List<Currency> currencies;
    private int success;
    private string redirectUrl = "AccountManagement.aspx";

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        accountService = ServiceFactory.Create<IAccountService>();
        accountTypeService = ServiceFactory.Create<IAccountTypeService>();
        currencyService = ServiceFactory.Create<ICurrencyService>();

        GetParams();
        LoadData();
        AddRules();
        if (!IsPostBack)
        {
            UpdateView();
        }
    }

    protected void AddOrUpdate(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            bool isAdd = false;
            if (account == null)
            {
                isAdd = true;
                account = new Account();
            }
            account.DisplayName = tbDisplayName.Text;
            account.Currency = currencyService.GetCurrencyById(int.Parse(cbAccountType.SelectedValue));
            account.AccountType1 = accountTypeService.GetAccountTypeById(int.Parse(cbCurrencyType.SelectedValue));
            account.ContactName = tbContactName.Text;
            account.ContactPhone = tbContactPhone.Text;
            account.Description = tbDescription.Text;

            if (isAdd)
            {
                account.InitBalance = decimal.Parse(tbInitBalance.Text);
                account.UserId = WebSession.CurrentUser.UserId;
                accountService.CreateAccount(account);
                success = 1;
            }
            else
            {
                accountService.UpdateAccount(account);
                success = 2;
            }
            Response.Redirect(string.Format("{0}?success={1}", redirectUrl, success));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        accountId = QueryStringUtility.GetInt32("accountId", 0);
    }

    private void LoadData()
    {
        try
        {
            if (accountId != 0)
                account = accountService.GetAccountById(accountId);
            accountTypes = accountTypeService.GetAccountTypes();
            currencies = currencyService.GetCurrencies();
        }
        catch
        {
            account = null;
        }
    }

    private void UpdateView()
    {
        AddAccountTypes();
        AddCurrencies();
        (Master as Admin_MasterPage).SelectedTab = "accountManagement";
        if (account == null)
        {
            pageTitle.Text = Resources.Strings.AddAccount;  
            btAddOrUpdate.Text = Resources.Strings.Add;
        }
        else
        {
            pageTitle.Text = Resources.Strings.UpdateAccount;
            btAddOrUpdate.Text = Resources.Strings.Update;

            tbDisplayName.Text = account.DisplayName;
            cbCurrencyType.SelectedValue = account.CurrencyType.ToString();
            cbAccountType.SelectedValue = account.CurrencyType.ToString();
            tbInitBalance.Text = account.InitBalance.HasValue == true ? account.InitBalance.Value.ToString(MoneyManagerConfig.CurrencyFormat) : "0";
            tbDescription.Text = account.Description;
            tbContactName.Text = account.ContactName;
            tbContactPhone.Text = account.ContactPhone;
            tbInitBalance.Enabled = false;
        }
    }

    private void AddCurrencies()
    {
        cbCurrencyType.DataSource = currencies;
        cbCurrencyType.DataTextField = "Name";
        cbCurrencyType.DataValueField = "Id";
        cbCurrencyType.DataBind();
    }

    private void AddAccountTypes()
    {
        cbAccountType.DataSource = accountTypes;
        cbAccountType.DataTextField = "Name";
        cbAccountType.DataValueField = "Id";
        cbAccountType.DataBind();
    }

    private void AddRules()
    {
        if (account == null)
        {
            validationManager.AddRule(new ValueRequiredRule(tbInitBalance, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
            validationManager.AddRule(new DecimalValueRule(tbInitBalance, Strings.DecimalValueErrorMessage, Strings.DecimalValueHint));
        }
        validationManager.AddRule(new ValueRequiredRule(tbDisplayName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbContactName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbContactPhone, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));

        validationManager.Notifier = new BalloonNotifier();
    }


    #endregion

    #region public functions


    #endregion

}
